04. Routes & POST requests

Routes & POST requests Heading

Routes: POST Requests

Routes & POST intro

One way to collect and store user data so that you can access it later is through making an HTTP POST request. Analogous to the .get() Express method, there is also a .post() method to handle HTTP POST requests. An HTTP POST request sends data to the project's endpoint, where it is stored and can be accessed through a GET request, which we covered in the last lesson. Here is what a simple POST request could look like using the Express method .post():

// POST method route
app.post('/', function (req, res) {
  res.send('POST received')
})

ND#0001 C3 L3 A02 Routes And Post Requests

Here is how you could setup a basic POST route in the server side code.

First, Create an array to hold data:

const data = []

Then, create post() with a url path and a callback function:

app.post('/addMovie', addMovie )

In the callback function, add the data received from request.body. This is the key piece of information we are interested in from that long stretch of data we saw previously that the request (req) argument returns.

function addMovie (req, res){
   console.log(req.body)
   data.push(req.body)
}

In the next section we will cover how to execute this POST route with a request from the client side code.

Routes & POST requests Quiz

Check all the following that are true about Express Routes and HTTP Requests

SOLUTION:
  • Express routes are setup in the server code
  • Express provides handlers that directly correspond to HTTP requests, such as GET and POST.
  • GET and POST requests are used to retrieve and save data between APIs and servers.

Routes & POST requests Quiz 2

QUESTION:

Write a post route for adding a favorite ice cream flavor via the path ’/flavor’ to an array named data.

const data = [];
// Complete the POST route started for you by adding the route as the first parameter below:
app.post(_,addFlavor);
function addFlavor(req,res){
// And by adding the body of the request to the `data` array on the line below

};

SOLUTION:

NOTE: The solutions are expressed in RegEx pattern. Udacity uses these patterns to check the given answer

Routes & POST requests Summary

So how do we actually activate our POST route, and how does it know what data to process? We will learn the answer to those questions in the next lesson, when we learn about connecting client and server code. So far we have just been working on the server side, but next we will learn how to POST and GET data from the browser.

Routes & POST requests Example Prep

Here is how you could setup a basic POST route in the server side code.

First, Create an array to hold data:

const data = []

Then, create post() with a url path and a callback function:

app.post('/addMovie', addMovie )

In the callback function, add the data received from request.body. This is the key piece of information we are interested in from that long stretch of data we saw previously that the request (req) argument returns.

function addMovie (req, res){
   console.log(req.body)
   data.push(req.body)
}

In the next section we will cover how to execute this POST route with a request from the client side code.